package com.ch.engine.daily.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ExcelRightCellFinder {

    public static void main(String[] args) throws IOException {
        String filePath = "C:\\Users\\Administrator\\Desktop\\111维信通2期\\三表线上化\\111.xlsx";
        String target = "金额";

        FileInputStream fis = new FileInputStream(new File(filePath));
        Workbook workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);

        boolean found = false;

        for (Row row : sheet) {
            for (Cell cell : row) {
                if (cell.getCellType() == CellType.STRING && cell.getStringCellValue().equals(target)) {
                    int columnIndex = cell.getColumnIndex();
                    Cell rightCell = row.getCell(columnIndex + 1);
                    if (rightCell != null) {
                        System.out.println("右边的单元格内容是: " + getCellValue(rightCell));
                    } else {
                        System.out.println("右边的单元格为空或不存在。");
                    }
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
        }
        workbook.close();
        fis.close();
    }

    private static String getCellValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                return String.valueOf(cell.getNumericCellValue());
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                // 可选：你也可以选择执行公式的结果
                return cell.getCellFormula();
            case BLANK:
                return "";
            default:
                return "";
        }
    }
}
